package com.zhongkun.datahome.service.domain;

import com.zhongkun.datahome.common.xss.Xss;
import com.zhongkun.datahome.service.constant.SkuStatusEnum;
import com.zhongkun.datahome.service.processor.dto.SkuDto;
import com.zhongkun.datahome.service.req.cmd.sku.SkuCmd;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;

import java.text.MessageFormat;

/**
 * 商品信息对象 sku
 * 
 * @author ruoyi
 * @date 2025-04-14
 */
@Data
public class Sku extends BaseEntity {
    @Length(max = 64, message = "商品名称不能超过{max}个字符")
    @Xss(message = "商品名称不能包含脚本字符")
    @ApiModelProperty("商品名称")
    private String skuName;

    @ApiModelProperty("商品状态:0已下架，1已上架")
    private Integer status;

    @Xss(message = "分类不能包含脚本字符")
    @ApiModelProperty("分类")
    private String category;

    @Xss(message = "品牌不能包含脚本字符")
    @ApiModelProperty("品牌")
    private String brandName;

    @Xss(message = "单位不能包含脚本字符")
    @ApiModelProperty("单位")
    private String unit;

    @Xss(message = "型号规格不能包含脚本字符")
    @ApiModelProperty("型号规格")
    private String specification;

    @ApiModelProperty("商品来源")
    private String source;

    @ApiModelProperty("备注")
    private String remark;

    public static Sku convert(SkuCmd cmd) {
        Sku sku = new Sku();
        sku.update(cmd);
        return sku;
    }

    public void update(SkuCmd cmd) {
        this.setSkuName(cmd.getSkuName());
        this.setUnit(cmd.getUnit());
        this.setBrandName(cmd.getBrandName());
        this.setSpecification(cmd.getSpecification());
        this.setSource(cmd.getSource());
        this.setRemark(cmd.getRemark());
        this.setStatus(SkuStatusEnum.ON_SHELF.getStatus());
        this.setCategory(cmd.getCategory());
    }

    public String getKey(){
        return MessageFormat.format("{0}-{1}-{2}-{3}-{4}", this.getSkuName(), this.getCategory(), this.getUnit(), this.getBrandName(), this.getSpecification());
    }

    public static Sku convert(SkuDto dto) {
        Sku sku = new Sku();
        sku.setSkuName(dto.getSkuName());
        sku.setUnit(dto.getUnit());
        sku.setBrandName(dto.getBrandName());
        sku.setSpecification(dto.getSpecification());
        sku.setSource("back_import");
        sku.setRemark(dto.getRemark());
        sku.setStatus(SkuStatusEnum.ON_SHELF.getStatus());
        sku.setCategory(dto.getCategory());
        return sku;
    }

    public boolean getKeyChangeFlag(SkuCmd cmd) {
        return !StringUtils.equals(this.getSkuName(), cmd.getSkuName()) ||
                !StringUtils.equals(this.getUnit(), cmd.getUnit()) ||
                !StringUtils.equals(this.getBrandName(), cmd.getBrandName()) ||
                !StringUtils.equals(this.getSpecification(), cmd.getSpecification()) ||
                !StringUtils.equals(this.getCategory(), cmd.getCategory());
    }
}
